package user.com.controller;


import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.constant.ResponseDataConstant;
import com.responseData.R;
import com.responseData.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import user.com.aspect.FormatFileURL;
import user.com.entity.User;
import user.com.entity.Dto.UserLoginDto;
import user.com.entity.Vo.UserLoginVo;
import user.com.service.MsgService;
import user.com.service.UserService;
import user.com.utils.RedisUtils;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwj
 * @since 2023-11-11
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MsgService msgService;


    /**
     * 根据uid 获取用户信息
     * @param userId 用户id
     * @return
     */
    @GetMapping("/getById/{userId}")
    @FormatFileURL(value = {"avatar"})
    public R getUserInfo(@PathVariable String userId){
        UserLoginDto dto = new UserLoginDto();
        try {
            dto = userService.getUserInfoByUid(userId);
        }catch (Exception e){
            e.printStackTrace();
            log.info("系统角色异常");
            return ResponseData.error("获取失败");
        }

        return ResponseData.success(dto);
    }

    /**
     * 使用密码登录
     * @param map
     * @return
     */
    @PostMapping("/loginWithPass")
    public R loginWithPass(@RequestBody UserLoginVo map){
        UserLoginDto userLoginDto = null;
        try{
            userLoginDto = userService.loginWithPass(map);
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return ResponseData.error(e.getMessage());
        }

        return ResponseData.success(userLoginDto);
    }


    /**
     * 用户短信登录 未注册则对其进行注册
     * @param map
     * @return
     */
    @PostMapping("/loginWithMsg")
    public R loginWithMsg(@RequestBody UserLoginVo map){
        UserLoginDto userLoginDto = null;
        try{
            userLoginDto = userService.loginWithMsg(map);
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return ResponseData.error(e.getMessage());
        }

        return ResponseData.success(userLoginDto);
    }

    /**
     * 发送注册/登录 邮件验证码
     * @param email
     * @return
     */
    @GetMapping("/getVerifyCode/{email}")
    public R getVerifyCode(@PathVariable String email){
        try{
            msgService.sendMsg(email);
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return  ResponseData.error("发送失败");
        }
        return ResponseData.success("发送成功");
    }

    /**
     * 用户注销
     * @param token
     * @return
     */
    @GetMapping("/loginOut/{token}")
    public R loginOut(@PathVariable String token){
        try{
            if(token == null){
                return ResponseData.error("传入参数为空");
            }
            RedisUtils.redis.delete(token);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("注销失败");
        }

        return ResponseData.success("注销成功");
    }

    @GetMapping("/getUserInfoByToken/{token}")
    public R getInfoByToken(@PathVariable String token){
//        Map<String, Object> videoInfo = null;
//        try{
//            videoInfo = UserInfoUtils.getUserInfo();
//        }catch (Exception e){
//            e.printStackTrace();
//            log.info(e.getMessage());
//            return ResponseData.unusualCustomize(ResponseDataConstant.LOGIN_TIMEOUT,"用户信息异常");
//        }

        return ResponseData.success("");
    }
}

